'''
Created on Jan 24, 2011

@author: Administrator
'''
import unittest
from mock import Mock

from sniffylib.util import uiutil

SERVICE_TEMPL = 'svc%d'

def create_and_register(updater, idx):
    service = Mock()
    service.state = False
    name = SERVICE_TEMPL % idx
    updater.register_service(name, service)
    return service

def services_to_text(services):
    lines = [uiutil.state_to_text(SERVICE_TEMPL % idx, svc.state) for idx, svc in services]
    return uiutil.LINE_SEP.join(lines)

class Test(unittest.TestCase):


    def testStatusUpdater(self):
        def update_cb(t):
            global text
            text = t
        updater = uiutil.TextStatusUpdater(update_cb)
        services = [create_and_register(updater, i) for i in range(2)]
        exp_text = services_to_text(enumerate(services))
        self.assertEquals(exp_text, text)
        services[0].state = True
        services[0].state_cb(True)
        exp_text = services_to_text(enumerate(services))
        self.assertEquals(text, exp_text)


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()